package ciyuan.Block.Machine.Simple.CastTable;

import ciyuan.Block.Machine.Simple.SimpleMachineRegister;
import ciyuan.Block.TypeInterface.MachineTileEntity;
import ciyuan.Item.ItemRegisterManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class CastTableTileEntity extends MachineTileEntity {

    private static final String STONEPOT_OUT = "cystonepotout";
    private static final ITextComponent TITLE = new TranslationTextComponent("block.ciyuanrpg.cast_table");

    public CastTableTileEntity() {
        super(SimpleMachineRegister.CAST_TABLE_TILE_ENTITY.get(), 4, TITLE, 800);
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new CastTableContainer(SimpleMachineRegister.CAST_TABLE_CONTAINER.get(), sycID, inventory, getBlockPos());
    }

    @Override
    public void tick() {
        if (!this.hasLevel() || this.level.isClientSide) {
            return;
        }

        ItemStack potItemStack = this.inventory.getItem(0);
        ItemStack waterItemStack = this.inventory.getItem(1);

        if (potItemStack.isEmpty() || waterItemStack.isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (!potItemStack.getItem().equals(ItemRegisterManager.stonepot1.get())) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (!waterItemStack.getItem().equals(ItemRegisterManager.stonepot2.get())) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (potItemStack.getTag() == null) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (getOutput(potItemStack) == null) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (this.getProgress() >= this.getConsumeTime()) {
            ItemStack itemStack = new ItemStack(getOutput(potItemStack));
            if (this.inventory.canAddItem(itemStack)) {
                this.inventory.addItem(itemStack);
                this.inventory.addItem(new ItemStack(ItemRegisterManager.stonepot.get()));
                potItemStack.setCount(potItemStack.getCount() - 1);
                this.resetProgress();
            }
        } else if (!this.consumeFuel() && !waterItemStack.isEmpty()) {
            waterItemStack.setCount(waterItemStack.getCount() - 1);
            this.addFuel(400);
            this.consumeFuel();
            this.setMachineStatus(true);
        }
    }

    private static Item getOutput(ItemStack input) {
        String descriptionId = input.getTag().getString(STONEPOT_OUT);
        if (descriptionId.equals(ItemRegisterManager.Aluminium.get().getDescriptionId())) {
            return ItemRegisterManager.Aluminium.get();
        } else if (descriptionId.equals(ItemRegisterManager.Copper.get().getDescriptionId())) {
            return ItemRegisterManager.Copper.get();
        } else if (descriptionId.equals(Items.GOLD_INGOT.getDescriptionId())) {
            return Items.GOLD_INGOT;
        } else if (descriptionId.equals(Items.IRON_INGOT.getDescriptionId())) {
            return Items.IRON_INGOT;
        } else if (descriptionId.equals(ItemRegisterManager.tin.get().getDescriptionId())) {
            return ItemRegisterManager.tin.get();
        }
        return null;
    }

}
